<template>
  <div class="loginbody">
    <div class="logindata">
      <div class="logintext">
        <h2>Welcome To MaDo</h2>
      </div>
      <div class="formdata">
        <el-form ref="form" :model="form" :rules="rules">
          <el-form-item prop="username">
            <el-input
              v-model="form.username"
              clearable
              placeholder="请输入账号"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="form.password"
              clearable
              placeholder="请输入密码"
              show-password
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div class="tool">
        <div>
          <el-checkbox v-model="checked" @change="remenber">记住我</el-checkbox>
        </div>
        <div>
          <span class="shou" @click="forgetpas">忘记密码？</span>
        </div>
      </div>
      <div class="butt">
        <el-button type="primary" @click.native.prevent="login('form')">登录</el-button>
        <el-button class="shou" @click="register">注册</el-button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "login",
  data() {
    return {
      // 账号密码
      form: {
        password: "",
        username: "",
      },
      // 电影权限
      films:0,
      // 小说权限
      fiction:0,
      // 作家权限
      writer:0,
      // 是否记住我
      checked: false,
      // 表单校验
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { max: 10, message: "不能大于10个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { max: 10, message: "不能大于10个字符", trigger: "blur" },
        ],
      },
    };
  },

  methods: {
    login(ev) {
      //自动开启表单校验
      this.$refs.form.validate((valid) => {
        // 判断表单是否校验正确
        if (valid) {
          //开启忙等框
          this.logining = true;
          // 发请求
          this.$http.post("/login",this.form)
            .then(result=>{
              result = result.data;
              console.log(result)
              console.log(result.power)
              console.log(result.power.fiction)
              //关闭忙等框
              // 存名字
              sessionStorage.setItem('user', JSON.stringify(this.form.username));
              // 存小说权限
              sessionStorage.setItem('fiction', JSON.stringify(result.power.fiction));
              // 存作家权限
              sessionStorage.setItem('writer', JSON.stringify(result.power.writer));
              // 电影权限
              sessionStorage.setItem('movie', JSON.stringify(result.power.movie));
              this.$router.push({ path: '/' });
            })
            .catch(result=>{
              this.$message({
                message: "账号或密码错误！！！",
                type: 'error'
              });
            })
        } else {
          console.log('请输入账号和密码!!');
          return false;
        }
      });
    },
    /*login(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          login(this.form)
            .then((res) => {
              if (res.code === 200) {
                setToken(res.data.token);
                localStorage.setItem("USERNAME", res.data.username);
                this.$message({
                  message: "登录成功啦",
                  type: "success",
                  showClose: true,
                });
                this.$router.replace("/");
              } else {
                this.$message({
                  message: "账户名或密码错误",
                  type: "error",
                  showClose: true,
                });
              }
            })
            .catch((err) => {
              this.$message({
                message: "账户名或密码错误",
                type: "error",
                showClose: true,
              });
            });
        } else {
          return false;
        }
      });
    },*/
    remenber(data){
      // 判断是否勾选
      this.checked=data
      if(this.checked){
        // 勾选记住我就存一个localStorage的news
        localStorage.setItem("news",JSON.stringify(this.form))
      }else{
        // 没勾选记住我就删除存在localStorage的news
        localStorage.removeItem("news")
      }
    },
    forgetpas() {
      this.$message({
        type:"info",
        message:"功能尚未开发额😥",
        showClose:true
      })
    },
    register() {
      this.$message({
        type:"info",
        message:"功能尚未开发额😥",
        showClose:true
      })
    },
    // 记住我
    rememberMe() {
      if(localStorage.getItem("news")){
        this.form=JSON.parse(localStorage.getItem("news"))
        this.checked=true
      }
    }
  },
  mounted() {
    // 记住我
    this.rememberMe();
  },
};
</script>

<style scoped>
.loginbody {
  width: 100%;
  height: 100%;
  min-width: 1000px;
  background-image: url("../assets/md/13.jpg");
  background-size: 100% 100%;
  background-position: center center;
  overflow: auto;
  background-repeat: no-repeat;
  position: fixed;
  line-height: 100%;
  padding-top: 150px;
}

.logintext {
  margin-bottom: 20px;
  line-height: 50px;
  text-align: center;
  font-size: 30px;
  font-weight: bolder;
  color: white;
  text-shadow: 2px 2px 4px #000000;
}

.logindata {
  width: 400px;
  height: 300px;
  transform: translate(-50%);
  margin-left: 50%;
}

.tool {
  display: flex;
  justify-content: space-between;
  color: #606266;
}

.butt {
  margin-top: 10px;
  text-align: center;
}

.shou {
  cursor: pointer;
  color: #606266;
}

/*ui*/
/* /deep/ .el-form-item__label {
  font-weight: bolder;
  font-size: 15px;
  text-align: left;
}

/deep/ .el-button {
  width: 100%;
  margin-bottom: 10px;

} */
</style>
